package kuba.configuration;

import kuba.aspects.Aspects;
import kuba.common.BeanLogger;
import kuba.common.BeanValidator;
import kuba.daos.AuthorDAO;
import kuba.daos.AuthorSqlDAO;
import kuba.daos.AuthorXmlDAO;
import kuba.daos.CdDAO;
import kuba.daos.CdSqlDAO;
import kuba.daos.CdXmlDAO;
import kuba.event.BasicEvent;
import kuba.event.EventHandler;
import kuba.event.EventPublisher;
import kuba.gui.ButtonListener;
import kuba.gui.MainWindow;
import kuba.service.Service;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@EnableAspectJAutoProxy
public class ApplicationConfiguration {

    @Bean
    public PropertyPlaceholderConfigurer getProperties() {
        PropertyPlaceholderConfigurer configurer=new PropertyPlaceholderConfigurer();
        configurer.setLocations(new Resource[]{new ClassPathResource("application.properties")});
        return configurer;
    }
    
    @Bean
    public Aspects getAspects() {
        return new Aspects();
    }

    @Bean
    @Profile("mysql")
    public MySqlConfiguration getMySqlConfiguration() {
        return new MySqlConfiguration();
    }
    @Bean
    @Profile("mysql")
    public JdbcTemplate getJdbcTemplate() {
        BasicDataSource dataSource = new BasicDataSource();
        MySqlConfiguration configuration = getMySqlConfiguration();
        dataSource.setUrl(configuration.getUrl());
        dataSource.setPassword(configuration.getPassword());
        dataSource.setUsername(configuration.getLogin());
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return new JdbcTemplate(dataSource);
    }
    @Bean
    @Profile("mysql")
    public CdDAO getCdDao() {
        return new CdSqlDAO();
    }
    @Bean
    @Profile("mysql")
    public AuthorDAO getAuthorDAO() {
        return new AuthorSqlDAO();
    }

    @Bean
    @Profile("xml")
    public CdDAO getCdXmlDao() {
        return new CdXmlDAO();
    }
    @Bean
    @Profile("xml")
    public AuthorDAO getAuthorXmlDAO() {
        return new AuthorXmlDAO();
    }

    @Bean
    public Service getService() {
        return new Service();
    }

    @Bean
    public ButtonListener getButtonListener() {
        return new ButtonListener();
    }

    @Bean
    public MainWindow getMainWindow() {
        return new MainWindow();
    }

    @Bean
    public BeanValidator getBeanValidator() {
        return new BeanValidator();
    }
    @Bean
    public BeanLogger getBeanLogger() {
        return new BeanLogger();
    }
    @Bean
    public ApplicationListener<BasicEvent> getaApplicationListener(){
        return new EventHandler();
    }
    @Bean
    public EventPublisher getEventPublisher() {
        return new EventPublisher();
    }

}
